import 'package:flutter/services.dart';
import 'package:ycbl/utils/toast_util.dart';

class GenseeLivePlugin {

  /// native 端的名字要和这里一致，才能成功匹配
  final _channel = const MethodChannel('gensee_live_plugin');

  static GenseeLivePlugin _instance;
  factory GenseeLivePlugin() => _instance ??= GenseeLivePlugin._();

  GenseeLivePlugin._() {
    _channel.setMethodCallHandler(_handleMessages);
  }

  ///  处理 Native 端主动调用的方法；  Future 是返回值
  Future<Null> _handleMessages(MethodCall call) async {
  }

  /// 开启直播
  startLive(String roomNumber, String joinPassword, String userName) async {
    _tryInvoke('startLive', roomNumber, joinPassword, userName);
  }

  startVod(String roomNumber, String joinPassword, String userName) async{
    _tryInvoke('startVod', roomNumber, joinPassword, userName);
  }

  _tryInvoke(String method, String roomNumber, String joinPassword, String userName) async{
    try {
      var args = _prepareArgs(roomNumber, joinPassword, userName);
      await _channel.invokeMethod(method, args);
    } catch (e) {
      ToastUtil.showShortToast(e.message ?? e.toString());
    }
  }

  Map<String, dynamic> _prepareArgs(String roomNumber, String joinPassword, String userName) {

    /// Map 传参
    final args = <String, dynamic>{
      'roomNumber': roomNumber,     ///房间号
      'joinPassword': joinPassword, ///加入密码
      'userName': userName,         ///昵称 or 用户名
    };

    return args;
  }

}